<template>
  <div>
    <el-card>
      <div>
        <el-row type="flex" justify="space-between">
          <el-button type="primary" size="small" @click="addAgent">添加经纪</el-button>
          <el-col :span="12">
            <el-input>
              <el-button slot="append" icon="el-icon-search" />
            </el-input>
          </el-col>
        </el-row>
      </div>
      <div class="table-content">
        <el-table :data="tableData" border>
          <el-table-column label="图片" prop="image">
            <template v-slot="{row}">
              <img style="width:180px" :src="row.image" alt="">
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="邮箱" prop="email" />
          <el-table-column label="电话" prop="phone" />
          <el-table-column label="地址" prop="address" />
          <el-table-column label="介绍" prop="desc" />
          <el-table-column label="操作">
            <el-button size="small" type="primary">编辑</el-button>
            <el-button size="small" type="danger">删除</el-button>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">
          <el-pagination
            layout="prev, pager, next"
            :total="50"
          />
        </el-row>
      </div>
    </el-card>
    <el-dialog title="新增经纪" :visible.sync="showDialog">
      <el-form label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="formData.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="formData.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="formData.phone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="formData.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="formData.desc" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="封面">
          <ImageUpload ref="uploadImg" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="confirm">确认</el-button>
          <el-button size="small">取消</el-button>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAgents, addAgents } from '@/api/agent'
export default {
  data() {
    return {
      showDialog: false,
      tableData: [],
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
    }
  },
  async created() {
    const res = await getAgents({
      _limit: 2,
      _start: 0
    })
    this.tableData = res.data
    console.log(80, res.data)
    // 封面回显
    this.$refs.uploadImg.fileList.push({
      url: this.formData.image
    })
  },
  methods: {
    addAgent() {
      this.showDialog = true
    },
    async confirm() {
      const upload = this.$refs.uploadImg
      if (upload.percent > 0 && upload.percent < 100) {
        return this.$message({
          type: 'warning',
          message: '图片正在上传中，请稍后保存'
        })
      }
      this.formData.image = upload.fileList[0] && upload.fileList[0].url
      // 调用接口 新增
      const res = await addAgents(this.formData)
      console.log(114, res)
    }
  }
}
</script>

<style>
.table-content{
  margin-top: 10px;
}
</style>
